﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BLL;

namespace Demo
{
    public partial class FrmDeparmentAdd : Form
    {
        DepartmentModel _departmentmodel = new DepartmentModel();
        private string _ID = "";
        DepartmentBLL _bll = new DepartmentBLL();

        public FrmDeparmentAdd()
        {
            InitializeComponent();
            this.ControlBox = false;
        }        

        public FrmDeparmentAdd(string id)
        {
            InitializeComponent();
            this.ControlBox = false;
            _ID = id;
            this.Text = "修改部门信息";
        }       

        private void FrmDeparmentAdd_Load(object sender, EventArgs e)
        {
            if (_ID != "")
            {
                loadData();
            }
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnSave_Click(object sender, EventArgs e)
        {
            Save();
        }        

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            //判断必填项有没有填
            int flag = IsCheck();
            if (flag!=0)
            {
                if (flag==1)
                {
                    errorPDepartmentNo.SetError(txtDepartmentNo, "部门编码不为空");                  
                }
                else if (flag==2)
                {
                    errorPDepartmentName.SetError(txtDepartmentName, "部门名称不为空");
                }
                return;
            }
            //判断是新增还是修改
            if (_ID == "")
            {
                //根据部门编码判断此部门编码是否已经存在
                int isExist = _bll.IsExistByDepartmentNo(this.txtDepartmentNo.Text);
                if (isExist==-1)
                {
                    //不存在就给实体赋值
                    DepartmentModel departmentmodel =SetData();
                    departmentmodel.Id = Guid.NewGuid().ToString();
                    //添加数据到数据库
                    _bll.Insert(departmentmodel);                    
                }
                else
                {
                    MessageBox.Show("部门编码已存在，请重新输入部门编码");
                    return;
                }
            }
            else
            {
                //根据当前Id和部门编码判断此部门编码是否已经存在
                int isExist = _bll.IsExistByIdAndDepartmentNo(_ID,this.txtDepartmentNo.Text);
                if (isExist == -1)
                {
                    //不存在就给实体赋值
                    DepartmentModel departmentmodel = SetData();
                    departmentmodel.Id = _ID;
                    //添加数据到数据库
                    _bll.Update(departmentmodel);
                }
                else
                {
                    MessageBox.Show("部门编码已存在，请重新输入部门编码");
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
            MessageBox.Show("保存成功");
            this.Close();
        }

        /// <summary>
        /// 编辑时初始化加载数据
        /// </summary>
        public void loadData()
        {
            //根据传过来的ID加载数据
            _departmentmodel = _bll.Get(_ID);
            this.txtDepartmentNo.Text = _departmentmodel.DepartmentNo;
            this.txtDepartmentName.Text = _departmentmodel.DepartmentName;
            this.txtRemarks.Text = _departmentmodel.Remarks;
        }

        /// <summary>
        /// 给实体赋值
        /// </summary>
        /// <returns></returns>
        public DepartmentModel SetData()
        {
            DepartmentModel departmentmodel = new DepartmentModel();
            //不存在就给实体赋值
            departmentmodel.DepartmentNo = this.txtDepartmentNo.Text;
            departmentmodel.DepartmentName = this.txtDepartmentName.Text;
            departmentmodel.Remarks = this.txtRemarks.Text;
            return departmentmodel;
        }

        /// <summary>
        /// 检查必填项是否都填了
        /// </summary>
        /// <returns></returns>
        public int IsCheck()
        {
            errorPDepartmentNo.SetError(txtDepartmentNo,"");
            errorPDepartmentName.SetError(txtDepartmentName,"");
            if (this.txtDepartmentNo.Text == "")
            {
                return 1;
            }
            if (this.txtDepartmentName.Text == "")
            {
                return 2;
            }
            return 0;
        }

    }
}
